﻿
#region License, Terms and Conditions
//
// UserService.cs
//
// Authors: Kori Francis <twitter.com/djbyter>
// Copyright (C) 2012 Kori Francis. All rights reserved.
// 
// THIS FILE IS LICENSED UNDER THE MIT LICENSE AS OUTLINED IMMEDIATELY BELOW:
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//
#endregion

namespace Intercom.Core
{
    #region Imports
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    #endregion

    public class UserService : IntercomApiBase
    {
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="username">The API username to use when accessing the API</param>
        /// <param name="password">The API password to use when accessing the API</param>
        public UserService(string username, string password)
            : base(username, password)
        { }

        /// <summary>
        /// Retrieves a paginated list of all users in your application on Intercom.
        /// </summary>
        /// <returns>The paginated object if successful, null otherwise.</returns>
        public UserViewModel All()
        {
            return GetRequest<UserViewModel>("/users");
        }

        /// <summary>
        /// Retrieves a paginated list of all users in your application on Intercom.
        /// </summary>
        /// <param name="page">Optional, defaults to 1.</param>
        /// <param name="per_page">Optional, defaults to 500 (max of 500)</param>
        /// <returns>The paginated object if successful, null otherwise.</returns>
        public UserViewModel All(int? page, int? per_page)
        {
            if (page.HasValue && per_page.HasValue)
            {
                return GetRequest<UserViewModel>("/users?page={0}&per_page={1}", page.Value, per_page.Value);
            }
            else if (page.HasValue && !per_page.HasValue)
            {
                return GetRequest<UserViewModel>("/users?page={0}", page.Value);
            }
            else if (per_page.HasValue && !page.HasValue)
            {
                return GetRequest<UserViewModel>("/users?per_page={0}", per_page.Value);
            }
            else
            {
                return All();
            }
        }

        /// <summary>
        /// Retrieves a user. Expects either the email or user_id you used to create the user.
        /// </summary>
        /// <param name="user_id">The user_id used to create the user.</param>
        /// <returns>The found user if successful, null otherwise.</returns>
        public User Single(int user_id)
        {
            return GetRequest<User>("/users?user_id={0}", user_id);
        }

        /// <summary>
        /// Retrieves a user. Expects either the email or user_id you used to create the user.
        /// </summary>
        /// <param name="emailAddress">The email used to create the user.</param>
        /// <returns>The found user if successful, null otherwise.</returns>
        public User Single(string emailAddress)
        {
            return GetRequest<User>("/users?email={0}", emailAddress);
        }

        /// <summary>
        /// Creates a user. Expects a JSON object describing the user.
        /// </summary>
        /// <remarks>Social and geo location data is fetched asynchronously, so a secondary call to users will be required to fetch it.</remarks>
        /// <param name="user">The user to create</param>
        /// <returns>The user object if success, null otherwise.</returns>
        public User Create(User user)
        {
            return PostRequest<User, User>(user, "/users");
        }

        /// <summary>
        /// Updates a user. Expects a JSON object describing the user.
        /// </summary>
        /// <param name="user">The new user data to update</param>
        /// <returns>The updated user object if success, null otherwise.</returns>
        public User Update(User user)
        {
            return PutRequest<User, User>(user, "/users/");
        }
    }
}
